feat: fix tranche key ordering - #1081
Conversation
…ranche-key-ordering
| return nil | ||
| } | ||
|
|
||
| func reconstructLoExpirations(ctx sdk.Context, k dexkeeper.Keeper) error { |
There was a problem hiding this comment.
Why does this need to be a separate process? Why can't we just iterate through all limit order tranches and then check the tranche directly if it is expiring?
There was a problem hiding this comment.
I found explicit per-storage-branch functions easier to understand and safer: we iterate over the whole bunch of leafs at a time and leave no room for a mistake/misunderstanding. I could do as you suggested but in my opinion it would be a more complex migration code with no perceptible practical benefits like speed or efficiency
|
|
||
| // NewTrancheKey returns a new tranche key based on the tranche index. | ||
| func NewTrancheKey(trancheIdx uint64) string { | ||
| return fmt.Sprintf("tk-%020d", trancheIdx) |
There was a problem hiding this comment.
Probably not a big deal, but why not use Uint64ToSortableString? It's a bit more space-efficient. Also, from a UX perspective, trying to count/type a bunch of leading zeros is a PITA.
There was a problem hiding this comment.
no strong point against this approach, I also considered it, but preferred the zero-padded format for easier testing and debugging. re UX perspective: 1) I don't think anyone (but me and you in scope of this PR haha) would ever count the zeroes since it's easier and more useful to count the non-zero digits 2) if someone ever needs to get to know their tranche index, would it be easier for them to parse the base36 number prefixed with length?
There was a problem hiding this comment.
space efficiency is a good point. you suggest to use tk-Uint64ToSortableString(trancheIdx) instead of tk-020%d, right? do you guys think it worths refactoring?
There was a problem hiding this comment.
agree Uint64ToSortableString function, I would even get rid of the tk prefix - it doesnt seem to carry any useful information
There was a problem hiding this comment.
moved from zero padded tranche keys to Uint64ToSortableString usage. didn't remove the tk- prefix since it ruined proper lexicographical ordering: there are obsolete keys build with height + block gas usage that also have that base36 format build with Uint64ToSortableString and they all start with 5 as well as the tk-plain-decimal keys do. e.g. there is a tranche user record with tranche key 5ak8d741gqz, and after migration a typical tranche key reconstructed from plain decimal looks like 52ojj9. the former key is older than the latter whereas lexicographic sorting puts the latter one before the former, and this is incorrect and it should be mitigated: older tranche keys must be put before newer ones in sorting/iterating.
also addressed this comment:
It seems to me that this kind of migration should be performed in a numbered module migration rather than in an upgrade handler, just like any storage migration required for the correct operation of the new version of the code.
read about the results and dex module exported states here https://www.notion.so/hadron/dex-tk-0-migration-verification-35f85d6b9b1080e28044f4d4f793a285?source=copy_link#36e85d6b9b1080a996c4ef23fe9c7832
|
It seems to me that this kind of migration should be performed in a numbered module migration rather than in an upgrade handler, just like any storage migration required for the correct operation of the new version of the code. |
|
This all looks good. Maybe add a test to confirm that the 3 types of tranches keys (sortableString, tk-[n], and tk-[sortable-string] all get stored in the correct order. I think it is correct. But a test would be nice. |
There was a problem hiding this comment.
Am I understanding correctly that these are breaking changes? If an application stored the tk when it was created, it will unexpectedly receive an error when trying to work with the old tk later?
Does it make sense to add a note or comment about this in the code and mention it in a future release?
TrancheUserKey has a back-link to tk. It seems those need to be updated as well?
done here: 9c83710 |
doesn't this part of the migration address this? neutron/x/dex/migrations/v9/store.go Lines 195 to 229 in 9c83710 |
I guess so: some msgs and queries have tranche key as a parameter, so if e.g. a smart contract memorised a plain decimal tranche key e.g. tk-100 and it then tries to do something with that tranche using that stored key, it won't work because the tranche key will become tk-22S after migration. don't think it makes sense adding a comment in a code but a note in a future release makes sense to me |
this PR:
how to verify this code:
https://www.notion.so/hadron/dex-tk-0-migration-verification-35f85d6b9b1080e28044f4d4f793a285?source=copy_link#36e85d6b9b1080a996c4ef23fe9c7832